package com.hecun.crm.controller;

import com.hecun.crm.pojo.BaseDict;
import com.hecun.crm.pojo.Customer;
import com.hecun.crm.pojo.QueryVo;
import com.hecun.crm.service.BaseDictService;
import com.hecun.crm.service.CustomerService;
import com.hecun.crm.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private CustomerService customerService;

    @Value("${CUSTOMER_FROM_TYPE}")
    private String CUSTOMER_FROM_TYPE;
    @Value("${CUSTOMER_INDUSTRY_TYPE}")
    private String CUSTOMER_INDUSTRY_TYPE;
    @Value("${CUSTOMER_LEVEL_TYPE}")
    private String CUSTOMER_LEVEL_TYPE;

    @RequestMapping("list")
    public String queryCustomerList(Model model, QueryVo queryVo){
        //客户来源
        List<BaseDict> fromType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_FROM_TYPE);
        //所属行业
        List<BaseDict> industryType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_INDUSTRY_TYPE);
        //客户级别
        List<BaseDict> levelType = baseDictService.queryBaseDictByDictTypeCode(CUSTOMER_LEVEL_TYPE);

        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);

        if (StringUtils.isNotBlank(queryVo.getCustName())){
            try {
                queryVo.setCustName(new String(queryVo.getCustName().getBytes("ISO-8859-1"), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        Page<Customer> page = customerService.queryCustomerByQueryVo(queryVo);

        model.addAttribute("page", page);

        // 数据回显
        model.addAttribute("custName", queryVo.getCustName());
        model.addAttribute("custSource", queryVo.getCustSource());
        model.addAttribute("custIndustry", queryVo.getCustIndustry());
        model.addAttribute("custLevel", queryVo.getCustLevel());

        return "customer";
    }

    /**
     * 根据id查询用户,返回json对象
     * @param id
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public Customer queryCustomerById(Long id){
        Customer customer = customerService.queryCustomerById(id);
        return customer;
    }

    /**
     * 更新用户
     * @param customer
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public String updateCustomerById(Customer customer){
        customerService.updateCustomerById(customer);
        return "ok";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public String deleteCustomerById(Long id){
        customerService.deleteCustomerById(id);
        return "ok";
    }
}
